


import formula.calculator.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

public class KinematicEquation extends Activity {
        
        private RadioGroup g;
        private double answer = 0;
        private LinearLayout ll;
        private TextView tv;
        
        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.kinematic_equations_activity);
        ll = (LinearLayout)findViewById(R.id.testLinear);
        tv = new TextView(this);
        g = (RadioGroup)findViewById(R.id.variables);
    }
        
        private EditText initialVelocity;
        private String sInitialVelocity;
        private EditText finalVelocity;
        private String sFinalVelocity;
        private EditText acceleration;
        private String sAcceleration;
        private EditText displacement;
        private String sDisplacement;
        private EditText time;
        private String sTime;
         
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.solve:
                                switch (g.getCheckedRadioButtonId()) {
                                case R.id.initialVelocity:
                                        chooseEquation("initVel");
                                        break;
                                case R.id.finalVelocity:
                                        chooseEquation("finalVel");
                                        break;
                                case R.id.acceleration:
                                        chooseEquation("acceleration");
                                        break;
                                case R.id.time:
                                        chooseEquation("time");
                                        break;
                                case R.id.displacement:
                                        chooseEquation("displacement");
                                        break;
                                }
                }
        
                tv.setText("Answer: " + answer);
                ll.addView(tv);
        }
        
        private void chooseEquation(String str) {
                initialVelocity = (EditText) findViewById(R.id.initialVelocity);
                sInitialVelocity = initialVelocity.getText().toString();
                finalVelocity = (EditText) findViewById(R.id.finalVelocity);
                sFinalVelocity = finalVelocity.getText().toString();
                acceleration = (EditText) findViewById(R.id.acceleration);
                sAcceleration = acceleration.getText().toString();
                displacement = (EditText) findViewById(R.id.displacement);
                sDisplacement = displacement.getText().toString();
                time = (EditText) findViewById(R.id.initialVelocity);
                sTime = time.getText().toString();
                
                double initNum = 0;
                double finalNum = 0;
                double accelerationNum = 0;
                double timeNum = 0;
                double displacementNum = 0;
                
                if(!sInitialVelocity.equals("")) {
                        initNum = Double.parseDouble(sInitialVelocity);
                }
                if(!sFinalVelocity.equals("")) {
                        finalNum = Double.parseDouble(sFinalVelocity);
                }
                if(!sAcceleration.equals("")) {
                        accelerationNum = Double.parseDouble(sAcceleration);
                }
                if(!sTime.equals("")) {
                        timeNum = Double.parseDouble(sTime);
                }
                if(!sDisplacement.equals("")) {
                        displacementNum = Double.parseDouble(sDisplacement);
                }
                
                if(str.equals("initVel")) {
                        if(sFinalVelocity.equals("")) {
                                kinematic1Vi(displacementNum, timeNum, accelerationNum);
                        } else if(sAcceleration.equals("")) {
                                kinematic4Vi(finalNum, timeNum, displacementNum);
                        } else if(sDisplacement.equals("")) {
                                kinematic3Vi(initNum, accelerationNum, timeNum);
                        } else if(sTime.equals("")) {
                                kinematic2Vi(finalNum, accelerationNum, displacementNum);
                        }
                } else if(str.equals("finalVel")) {
                        if(sAcceleration.equals("")) {
                                kinematic4Vf(initNum, timeNum, displacementNum);
                        } else if(sDisplacement.equals("")) {
                                kinematic3Vf(initNum, accelerationNum, timeNum);
                        } else if(sTime.equals("")) {
                                kinematic2Vf(initNum, accelerationNum, displacementNum);
                        }
                } else if(str.equals("acceleration")) {
                        if(sFinalVelocity.equals("")) {
                                kinematic1a(displacementNum, initNum, timeNum);
                        } else if(sDisplacement.equals("")) {
                                kinematic3a(initNum, finalNum, timeNum);
                        } else if(sTime.equals("")) {
                                kinematic2a(initNum, finalNum, displacementNum);
                        }
                } else if(str.equals("time")) {
                        if(sAcceleration.equals("")) {
                                kinematic4t(initNum, finalNum, displacementNum);
                        } else if(sDisplacement.equals("")) {
                                kinematic3t(initNum, finalNum, accelerationNum);
                        }
                } else if(str.equals("displacement")) {
                        if(sAcceleration.equals("")) {
                                kinematic4x(initNum, finalNum, timeNum);
                        } else if(sTime.equals("")) {
                                kinematic2x(initNum, accelerationNum, timeNum);
                        } else if(sFinalVelocity.equals("")) {
                                kinematic1x(initNum, timeNum, accelerationNum);
                        }
                }

        }
        
/****                   Kinematic equations                             ****/

        /***            x = Vi * T + 1 / 2 * a * t^2            ***/
                        
        private void kinematic1x(double initialVelocity, double time, double acceleration) {
                double result = initialVelocity * time + (acceleration * (time * time)) / 2;
                answer = result;
        }
        
        private void kinematic1Vi(double displacement, double time, double acceleration) {
                double result = (displacement / time) - (acceleration * time / 2);
                answer = result;
        }
        
        private void kinematic1a(double displacement, double initialVelocity, double time) {
                double result = (2 * (displacement - (initialVelocity * time)) / (time * time));
                answer = result;
        }
        
        /***            Vf^2 = Vi^2 + 2 * a * x                 ***/
        
        private void kinematic2Vf(double initialVelocity, double acceleration, double displacement) {
                double result = Math.sqrt(initialVelocity * initialVelocity + 2 * acceleration * displacement);
                answer = result;
        }
        
        private void kinematic2Vi(double finalVelocity, double acceleration, double displacement) {
                double result = Math.sqrt(finalVelocity * finalVelocity - 2 * acceleration * displacement);
                answer = result;
        }
        
        private void kinematic2a(double initialVelocity, double finalVelocity, double displacement) {
                double result = (finalVelocity * finalVelocity - initialVelocity * initialVelocity) / (2 * displacement);
                answer = result;
        }
        
        private void kinematic2x(double initialVelocity, double finalVelocity, double acceleration) {
                double result = (finalVelocity * finalVelocity - initialVelocity * initialVelocity) / (2 * acceleration);
                answer = result;
        }
        
        /***            Vf = Vi + a * t                 ***/
        
        private void kinematic3Vf(double initialVelocity, double acceleration, double time) {
                double result = initialVelocity + acceleration * time;
                answer = result;
        }
        
        private void kinematic3Vi(double finalVelocity, double acceleration, double time) {
                double result = finalVelocity - acceleration * time;
                answer = result;
        }
        
        private void kinematic3a(double initialVelocity, double finalVelocity, double time) {
                double result = (finalVelocity - initialVelocity) / time;
                answer = result;
        }
        
        private void kinematic3t(double initialVelocity, double finalVelocity, double acceleration) {
                double result = (finalVelocity - initialVelocity) / acceleration;
                answer = result;
        }
        
        /***            x = 1/2(Vi + Vf) * t            ***/
        
        private void kinematic4x(double initialVelocity, double finalVelocity, double time) {
                double result = 1 / 2 * (initialVelocity + finalVelocity) * time;
                answer = result;
        }
        
        private void kinematic4t(double initialVelocity, double finalVelocity, double displacement) {
                double result = displacement / (1 / 2 * (initialVelocity * finalVelocity));
                answer = result;
        }
        
        private void kinematic4Vi(double finalVelocity, double time, double displacement) {
                double result = ((2 * displacement) / time) - finalVelocity;
                answer = result;
        }
        
        private void kinematic4Vf(double initialVelocity, double time, double displacement) {
                double result = ((2 * displacement) / time) - initialVelocity;
                answer = result;
        }
}

